import type { App } from 'vue';
import type { RouteRecordRaw } from 'vue-router';
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { setupRouterGuard } from './guard'
import { basicRoutes } from './routes'

const isHash = !!import.meta.env.VITE_APP_USE_HASH;

export const router = createRouter({
  history: isHash ? createWebHashHistory('/') : createWebHistory('/'),
  routes: basicRoutes as unknown as RouteRecordRaw[],
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name }: any = route
    router.hasRoute(name) && router.removeRoute(name)
  })
}

export function setupRouter(app: App<Element>) {
  app.use(router);
  setupRouterGuard(router);
}
